<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>OnlineBookstore后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo layui-hide-xs layui-bg-black">OnlineBookstore后台管理</div>
        <!-- 头部区域（可配合layui 已有的水平导航） -->
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                <a href="javascript:;">
                    <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                    <span id="realname">姓名</span>
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="javascript:;" class="resetPwd"><i class="layui-icon layui-icon-key"></i>修改密码</a></dd>
                    <dd><a href="javascript:;" id="logout"><i class="layui-icon layui-icon-logout"></i>退出</a></dd>
                </dl>
            </li>
        </ul>
    </div>
    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">订单管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" data-url="/html/order/list.html">订单列表</a></dd>
<!--                        <dd><a href="javascript:;" data-url="/html/order/list.html">数据统计</a></dd>-->
                    </dl>
                </li>
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">商品管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" data-url="/html/goods/list.html">商品列表</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">分类管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" data-url="/html/category/list.html">分类列表</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">用户管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" data-url="/html/user/list.html">用户列表</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">管理员管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" data-url="/html/admin/list.html">管理员列表</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>
    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px; height: 100%">
            <iframe id="main" style="width: 100%; height: 100%; border-width: 0px;"></iframe>
        </div>
    </div>
    <div class="layui-footer">
        <!-- 底部固定区域 -->
        底部固定区域
    </div>
</div>

<script src="/layui/layui.js"></script>
<script>
    //JS
    layui.use(['element', 'layer', 'util', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var util = layui.util;
        var $ = layui.$;
        var form = layui.form;

        getLoginAdmin();

        //退出按钮的事件
        $('#logout').click(() => {
            logout();
        });

        //左侧菜单点击事件
        $('.layui-nav-tree dd a').click(function () {
            //不能使用箭头函数
            const url = $(this).attr('data-url');
            if (url) {
                $('#main').attr('src', url);
            } else {
                layer.msg('无效的url');
            }

        });

        function logout() {
            //ajax
            $.ajax({
                url: '/admin/logout',
                dataType: 'json',
                success(result) {
                    layer.msg('退出登录！', {icon: 6}, () => {
                        window.location.href = "/html/admin/login.html";
                    });
                },
                error() {
                    layer.msg('请求失败', {icon: 5});
                }
            });
        }

        //获取当前登录的用户信息
        function getLoginAdmin() {
            $.ajax({
                url: '/admin/getLoginAdmin',
                type: 'get',
                dataType: 'json',
                success(result) {
                    if (result.code == 0) {
                        //有登录信息，在result.data中
                        if (result.data.realname) {
                            $('#realname').text(result.data.realname);
                        } else {
                            $('#realname').text(result.data.username);
                        }

                    } else {
                        //没有登陆
                        window.location.href = "/html/admin/login.html"
                    }
                },
                error() {
                    layer.msg("请求失败", {icon: 5}, () => {
                        window.location.href = "/html/admin/login.html"
                    });
                }

            });
        }

        //修改密码
        $('.resetPwd').click(function () {
            // 在此处输入 layer 的任意代码
            layer.open({
                type: 1, // page 层类型
                area: ['500px', '300px'],
                title: '修改密码',
                content:
                    '<div style="padding: 15px;">\
                      <form action="user?action=resetPwd" method="post">\
                        <div class="layui-form-item">\
                          <label class="layui-form-label">旧密码</label>\
                          <div class="layui-input-inline">\
                            <input class="layui-input" type="password" name="oldPwd" placeholder="请输入旧密码">\
                          </div>\
                        </div>\
                        <div class="layui-form-item">\
                          <label class="layui-form-label">新密码</label>\
                          <div class="layui-input-inline">\
                            <input class="layui-input" type="password" name="newPwd" placeholder="请输入新密码">\
                          </div>\
                        </div>\
                         <div class="layui-form-item">\
                          <label class="layui-form-label">确认密码</label>\
                          <div class="layui-input-inline">\
                            <input class="layui-input" type="password" name="rePwd" placeholder="请确认密码">\
                          </div>\
                        </div>\
                         <div class="layui-form-item">\
                          <div class="layui-input-block">\
                            <button class="layui-btn" lay-submit lay-filter="resetPwd">修改</button>\
                          </div>\
                        </div>\
                      </form>\
                    </div>'
            });
        })

        //表单的提交操作
        form.on('submit(resetPwd)', function (data) {
            //手动获取表单数据
            var oldPwd = $('[name="oldPwd"]').val();
            var newPwd = $('[name="newPwd"]').val();
            var rePwd = $('[name="rePwd"]').val();
            //ajax 发送修改密码的请求
            $.ajax({
                url: '/admin/reset',//请求路径
                data: {oldPwd, newPwd, rePwd},//要传递参数
                type: 'post',//http的请求方式
                dataType: 'json',//返回数据类型
                success: function (result) {//成功的回调 result服务器返回的数据
                    if (result.code == 0) {
                        //成功
                        layer.msg("修改成功", {icon: 6}, function (index) {
                            //跳转到登录页面
                            window.location.href = 'index.html';
                        });
                    } else {
                        //失败
                        layer.msg(result.msg, {icon: 5})
                    }
                },
                error: function () {
                    layer.msg('请求失败', {icon: 5});
                }
            });

            //阻止表单的自动提交
            return false;
        });
    });
</script>
</body>
</html>